package com.zhangzhan.gulimallauthserver.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.zhangzhan.gulimallauthserver.feign.MemberFeignService;
import com.zhangzhan.gulimallauthserver.tos.GiteeResTo;
import com.zhangzhan.gulimallauthserver.vos.GiteeUserInfoVo;
import com.zhangzhan.gulimallcommcon.tos.MemberTo;
import com.zhangzhan.gulimallcommcon.util.R;
import jakarta.servlet.http.HttpSession;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

import static com.zhangzhan.gulimallcommcon.constant.AuthConstant.SESSION_USER_INFO_KEY;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2024/6/28 3:52
 */
@Controller
@RequestMapping("/oauth2")
@ConfigurationProperties("oauth2.gitee")
@Data
public class Oauth2Controller {

    private String clientId;
    private String redirectUri;
    private String clientSecret;
    private String accessTokenUrl;
    private String userInfoUrl;

    @Autowired
    private MemberFeignService memberFeignService;

    /**
     * gitee
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/gitee/success", method = RequestMethod.GET)
    public String gitee(@RequestParam("code") String code, RedirectAttributes redirectAttributes, HttpSession httpSession) {

        //码云回调后返回code 获取access_key
        Map<String, Object> accessTokenBody = Map.of("grant_type", "authorization_code",
                "code", code,
                "client_id", clientId,
                "redirect_uri", redirectUri,
                "client_secret", clientSecret);

        String resOne = HttpUtil.post(accessTokenUrl, accessTokenBody);
        GiteeResTo giteeResTo = JSON.parseObject(resOne, GiteeResTo.class);

        if (Objects.isNull(giteeResTo) || StrUtil.isBlank(giteeResTo.getAccess_token())) {
            redirectAttributes.addFlashAttribute("giteeEerror", "网络错误");
            return "redirect:http://auth.gulimall.com/login.html";
        }

        //设置过期时间
        Calendar calendar = Calendar.getInstance();//new一个Calendar类,把Date放进去
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE, 1);//实现日期加一操作,也就是明天
        giteeResTo.setExpires_in(String.valueOf(calendar.getTime().getTime()));

        //获取用户信息
        Map<String, Object> userInfoBody = Map.of("access_token", giteeResTo.getAccess_token());
        String resTwo = HttpUtil.get(userInfoUrl, userInfoBody);
        GiteeUserInfoVo giteeUserInfoVo = JSON.parseObject(resTwo, GiteeUserInfoVo.class);

        if (Objects.isNull(giteeUserInfoVo) || Objects.isNull(giteeUserInfoVo.getId())) {
            redirectAttributes.addFlashAttribute("giteeEerror", "网络错误");
            return "redirect:http://auth.gulimall.com/login.html";
        }

        giteeUserInfoVo.setGiteeResTo(giteeResTo);

        R r = memberFeignService.oauth2GiteeEnrollUser(giteeUserInfoVo);
        if (r.getCode() != 0 || StrUtil.isBlank(r.getData())) {
            redirectAttributes.addFlashAttribute("giteeEerror", r.get("msg"));
            return "redirect:http://auth.gulimall.com/login.html";
        }
        String json = r.getData();
        MemberTo memberTo = JSON.parseObject(json, MemberTo.class);

        httpSession.setAttribute(SESSION_USER_INFO_KEY,memberTo);


        return "redirect:http://gulimall.com";
    }


}
